#!/bin/bash

# 服务管理脚本
# 用法: ./service_manager.sh [start_all|stop_all|restart_all|status|start_app|stop_app|restart_app|start_cache|stop_cache|restart_cache]

# 远程服务器信息
SERVER="root@182.160.6.140"
DEPLOY_PATH="/var/www/binance-analysis"

# 各服务使用的端口
APP_PORT=3000
CACHE_PORT=3001
MONITOR_PORT=3002
TD_MONITOR_PORT=3003

# 检查参数
if [ $# -lt 1 ]; then
  echo "用法: $0 [命令]"
  echo "可用命令:"
  echo "  start_all    - 启动所有服务"
  echo "  stop_all     - 停止所有服务"
  echo "  restart_all  - 重启所有服务"
  echo "  status       - 显示所有服务状态"
  echo "  start_app    - 只启动主应用 (端口: $APP_PORT)"
  echo "  stop_app     - 只停止主应用"
  echo "  restart_app  - 只重启主应用"
  echo "  start_cache  - 只启动缓存服务 (端口: $CACHE_PORT)"
  echo "  stop_cache   - 只停止缓存服务"
  echo "  restart_cache- 只重启缓存服务"
  echo "  start_monitor- 只启动监控服务 (端口: $MONITOR_PORT)"
  echo "  stop_monitor - 只停止监控服务"
  echo "  restart_monitor- 只重启监控服务"
  echo "  start_td_monitor- 只启动TD序列监控服务 (端口: $TD_MONITOR_PORT)"
  echo "  stop_td_monitor - 只停止TD序列监控服务"
  echo "  restart_td_monitor- 只重启TD序列监控服务"
  exit 1
fi

ACTION=$1

# 清理端口并启动应用的函数
clean_port_and_start_app() {
  local PORT=$1
  local APP_NAME=$2
  local SCRIPT_PATH=$3
  
  echo "清理${PORT}端口并启动${APP_NAME}..."
  ssh $SERVER "
    cd $DEPLOY_PATH && 
    
    # 停止旧的应用进程
    pm2 stop ${APP_NAME} || true
    pm2 delete ${APP_NAME} || true
    
    # 查找并杀死可能占用端口的进程
    PORT_PID=\$(lsof -t -i:${PORT}) 
    if [ ! -z \"\$PORT_PID\" ]; then
      echo \"发现进程 \$PORT_PID 占用端口${PORT}，正在终止...\"
      kill -9 \$PORT_PID
      sleep 1
    fi
    
    # 再次检查确保端口已释放
    PORT_PID=\$(lsof -t -i:${PORT})
    if [ ! -z \"\$PORT_PID\" ]; then
      echo \"仍有进程占用${PORT}端口，再次尝试终止...\"
      kill -9 \$PORT_PID
      sleep 2
    fi
    
    # 启动应用，传入环境变量设置端口
    PORT=${PORT} pm2 start ${SCRIPT_PATH} --name ${APP_NAME} && 
    pm2 save
  "
}

case $ACTION in
  start_all)
    echo "启动所有服务..."
    clean_port_and_start_app $APP_PORT "binance-analysis" "src/server.js"
    clean_port_and_start_app $CACHE_PORT "cache-updater" "scripts/update_cache.js"
    clean_port_and_start_app $MONITOR_PORT "monitor-app" "scripts/monitor.js"
    clean_port_and_start_app $TD_MONITOR_PORT "td-monitor" "scripts/monitor_td_sequence.js"
    ;;
  stop_all)
    echo "停止所有服务..."
    ssh $SERVER "pm2 stop all && pm2 save"
    ;;
  restart_all)
    echo "重启所有服务..."
    clean_port_and_start_app $APP_PORT "binance-analysis" "src/server.js"
    clean_port_and_start_app $CACHE_PORT "cache-updater" "scripts/update_cache.js"
    clean_port_and_start_app $MONITOR_PORT "monitor-app" "scripts/monitor.js"
    clean_port_and_start_app $TD_MONITOR_PORT "td-monitor" "scripts/monitor_td_sequence.js"
    ;;
  status)
    echo "所有服务状态:"
    ssh $SERVER "pm2 list"
    ;;
  start_app)
    echo "启动主应用..."
    clean_port_and_start_app $APP_PORT "binance-analysis" "src/server.js"
    ;;
  stop_app)
    echo "停止主应用..."
    ssh $SERVER "pm2 stop binance-analysis && pm2 delete binance-analysis && pm2 save"
    ;;
  restart_app)
    echo "重启主应用..."
    clean_port_and_start_app $APP_PORT "binance-analysis" "src/server.js"
    ;;
  start_cache)
    echo "启动缓存服务..."
    clean_port_and_start_app $CACHE_PORT "cache-updater" "scripts/update_cache.js"
    ;;
  stop_cache)
    echo "停止缓存服务..."
    ssh $SERVER "pm2 stop cache-updater && pm2 delete cache-updater && pm2 save"
    ;;
  restart_cache)
    echo "重启缓存服务..."
    clean_port_and_start_app $CACHE_PORT "cache-updater" "scripts/update_cache.js"
    ;;
  start_monitor)
    echo "启动监控服务..."
    clean_port_and_start_app $MONITOR_PORT "monitor-app" "scripts/monitor.js"
    ;;
  stop_monitor)
    echo "停止监控服务..."
    ssh $SERVER "pm2 stop monitor-app && pm2 delete monitor-app && pm2 save"
    ;;
  restart_monitor)
    echo "重启监控服务..."
    clean_port_and_start_app $MONITOR_PORT "monitor-app" "scripts/monitor.js"
    ;;
  start_td_monitor)
    echo "启动TD序列监控服务..."
    clean_port_and_start_app $TD_MONITOR_PORT "td-monitor" "scripts/monitor_td_sequence.js"
    ;;
  stop_td_monitor)
    echo "停止TD序列监控服务..."
    ssh $SERVER "pm2 stop td-monitor && pm2 delete td-monitor && pm2 save"
    ;;
  restart_td_monitor)
    echo "重启TD序列监控服务..."
    clean_port_and_start_app $TD_MONITOR_PORT "td-monitor" "scripts/monitor_td_sequence.js"
    ;;
  *)
    echo "未知操作: $ACTION"
    echo "用法: $0 [命令]"
    echo "运行 ./service_manager.sh 查看所有可用命令"
    exit 1
    ;;
esac

echo "操作完成！" 